/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package action;

import DAO.Constantes;
import DAO.DAO_Autor;
import actionForm.ModificarAutorActionForm;
import clases.Autor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Rafa
 */
public class ModificarAutorAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String CANCEL = "cancel";
    private static final String MODIFICAR = "modificar";
    private static final String FAILURE = "failure";

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        if (isCancelled(request)) {
            return mapping.findForward(CANCEL);
        } else {

            ModificarAutorActionForm formulario = (ModificarAutorActionForm) form;

            /*Se pone tambien en request la lista de usuarios libres para que esten 
             disponibles al añadir una nueva seccion o modificar una seccion*/
            DAO_Autor daoAutor = new DAO_Autor();
            if (formulario.getUpdate() == null) {
                String idAutor = formulario.getIdAutor();
                Autor autor = daoAutor.consultaAutorPorId(idAutor, true);

                formulario.setNombre(autor.getPersona().getNombre());
                formulario.setApellidos(autor.getPersona().getApellidos());
                formulario.setEmail(autor.getPersona().getEmail());
                
                return mapping.findForward(MODIFICAR);
            } else {
                if (formulario.getNombre().isEmpty() || formulario.getApellidos().isEmpty()
                        || formulario.getEmail().isEmpty()) {

                    formulario.setError(Constantes.getERROR_FORM());
                    return mapping.findForward(FAILURE);

                } else if (daoAutor.compruebaEmail(formulario.getEmail()) == false) {
                    formulario.setError("El email es incorrecto");
                    return mapping.findForward(FAILURE);

                } else {

                    String idAutor = formulario.getIdAutor();
                    /*String nombre = formulario.getNombre();
                    String apellidos = formulario.getApellidos();*/
                    String email = formulario.getEmail();

                                        
                    boolean ok = daoAutor.modificarAutor(idAutor,email, true);
                    if (ok == false) {
                        
                        formulario.setErrorAlert(Constantes.getERROR_FORM_MOD());
                    }
                    return mapping.findForward(SUCCESS);
                }
            }
        }

    }
}
